package org.example;

import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 王圆豪
 * Date: 2025-04-14
 * Time: 0:47
 */

public class Dome03_insert {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //获取数据库连接
            connection = DBUtil.getConnection();
            //定义sql语句
            String sql = "insert into student (name, email, class_id) values(?,?,?)";
            //获取预处理sql执行对象
            statement = connection.prepareStatement(sql);
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要插入的姓名：");
            String name = sc.next();
            System.out.println("请输入要插入的邮箱：");
            String email = sc.next();
            System.out.println("请输入要插入的班级编号：");
            long classId = Long.parseLong(sc.next());
            //用真实值代替占位符
            statement.setString(1, name);
            statement.setString(2, email);
            statement.setLong(3, classId);
            int row = statement.executeUpdate();
            if (row == 1) System.out.println("插入成功！");
            else System.out.println("查入失败！");
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
